package org.dtrd.modules.food.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.system.vo.LoginUser;
import org.dtrd.modules.food.entity.bean.RecipeDetail;
import org.dtrd.modules.food.entity.bean.RecipeListInfo;
import org.dtrd.modules.food.entity.po.DtrdExtEmsYwRecipes;
import org.dtrd.modules.food.entity.request.RecipePageRequest;
import org.dtrd.modules.food.mapper.DtrdExtEmsYwRecipesMapper;
import org.dtrd.modules.food.service.IDtrdExtEmsYwRecipesService;
import org.dtrd.modules.util.ShiroUtil;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 食谱 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-08
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwRecipesServiceImpl extends ServiceImpl<DtrdExtEmsYwRecipesMapper, DtrdExtEmsYwRecipes> implements IDtrdExtEmsYwRecipesService {

    private DtrdExtEmsYwRecipesMapper dtrdExtEmsYwRecipesMapper;

    @Override
    public List<RecipeDetail> getRecipesList() {
        LambdaQueryWrapper<DtrdExtEmsYwRecipes> queryWrapper = baseQueryWrapper();
        List<DtrdExtEmsYwRecipes> poList = list(queryWrapper);
        List<RecipeDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream()
                .map(po -> new RecipeDetail().parseFromPo(po))
                .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public RecipeDetail getRecipes(Integer dataId) {
        DtrdExtEmsYwRecipes dtrdExtEmsYwRecipes = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwRecipes)
                        .map(po -> new RecipeDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateRecipes(RecipeDetail dto) {
        DtrdExtEmsYwRecipes dtrdExtEmsYwRecipes = new DtrdExtEmsYwRecipes();
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if(dto.getDataId() == null){
            dtrdExtEmsYwRecipes = dtrdExtEmsYwRecipes.init();
            if (loginUser != null) {
                dtrdExtEmsYwRecipes.setRecipeCreater(loginUser.getId());
            }
        }
        dtrdExtEmsYwRecipes.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwRecipes);
    }

    @Override
    public boolean removeRecipes(Integer dataId) {
        DtrdExtEmsYwRecipes query = getById(dataId);
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if (loginUser != null) {
            query.setRecipeDeleter(loginUser.getId());
        }
        query.setRecipeDelTime(new Date());
        query.setIsDel(1);
        return updateById(query);
    }

    @Override
    public IPage<RecipeListInfo> pageRecipe(RecipePageRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwRecipes> queryWrapper = Wrappers.lambdaQuery(DtrdExtEmsYwRecipes.class);
        String recipeName = request.getRecipeName();
        if (StrUtil.isNotBlank(recipeName)) {
            queryWrapper.like(DtrdExtEmsYwRecipes::getRecipeName, StringUtil.fuzzySearchWrapper(recipeName));
        }
        Integer recipeCategoryId = request.getRecipeCategoryId();
        if (recipeCategoryId != null) {
            queryWrapper.eq(DtrdExtEmsYwRecipes::getRecipeCategoryId, recipeCategoryId);
        }
        IPage<DtrdExtEmsYwRecipes> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(RecipeListInfo::new);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwRecipes> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwRecipes.class)
                        .eq(DtrdExtEmsYwRecipes::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwRecipes> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwRecipes.class)
                        .eq(DtrdExtEmsYwRecipes::getIsDel, 0);
    }
}
